/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.general.bc;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.cip.esymons.general.be.Pais;

/**
 *
 * @author ADMINISTRADOR
 */
@Stateless
public class PaisFacade implements PaisFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Pais pais) {
        em.persist(pais);
    }

    public void edit(Pais pais) {
        em.merge(pais);
    }

    public void remove(Pais pais) {
        Pais aux = em.merge(pais);
        em.remove(aux);
    }

    public Pais find(Object id) {
        return em.find(Pais.class, id);
    }

    public List<Pais> findAll() {
        return em.createQuery("select object(o) from Pais as o order by o.paiDescripcion").getResultList();
    }

    public Pais buscaPeru(){
        try {
            return (Pais)em.createQuery("select object(o) from Pais as o where o.paiDescripcion like 'Per%'").getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

    public Pais buscaPaisXNombre(String pais){
        try {
            return (Pais)em.createQuery("select object(o) from Pais as o where o.paiDescripcion = ?1")
                    .setParameter(1, pais).getSingleResult();
        } catch (Exception e) {
            return null;
        }
    }

}
